import requestApi from '../requestApi';
import presetVar from '../presetVar';
import {toast} from 'nc-lightapp-front';

export default function searchClick(_props, searchVal, isDefaultSearch) {
    // props = this.props
    let props = _props
    if (!props || !props.transferTable) {
        props = this.props
    }
    const multiLang = props.MutiInit.getIntl(2011);
    const searchValue = props.search.getQueryInfo('20110MTAMN', false);
    if(true){
        const _pageInfo = props.transferTable.getTablePageInfo(presetVar.areaCode.billsArea,presetVar.areaCode.bodysArea)

        let data={
            querycondition:isDefaultSearch?(searchVal==null?{}:searchVal):searchValue.querycondition,
            pagecode: presetVar.pageCode.searchPage,
            pageInfo: {
                pageIndex: _pageInfo.pageIndex,
                pageSize: isDefaultSearch?10:_pageInfo.pageSize,
            },
            pagestatus:1,
            queryAreaCode:presetVar.areaCode.searchArea,  //查询区编码
            oid:'0001Z3100000000083QM',  //查询模板id，手工添加在界面模板json中，放在查询区，后期会修改,pub_area中查询areaid
            querytype:'tree',
            tradetype: props.getUrlParam("tradetype") || window.presetVar.tradetype
        };
        // let senddata = {data,"pagecode":presetVar.pagecode2,"tepmid":this.props.meta.getMeta().pageid};
        let senddata = {data,"pagecode":presetVar.pagecode2,"tepmid":props.meta.getMeta().pageid};
        requestApi.query({
            data:senddata,
            success: (res) => {
                const {data,pageInfo} = res
                let totalCount = 0;
                //是否是无金额申请
                const noAmount = props.search.getSearchValByField(presetVar.areaCode.searchArea, 'noamount')
                const isNoAmount = noAmount.value && noAmount.value.firstvalue
                //无金额 隐藏金额字段  orig_amount => head.items[] and mtapp_detail.items[]
                const metas = props.meta.getMeta()
                const filterKeys = ['rest_amount', 'orig_amount', 'pk_currtype']
                metas.head.items =  isNoAmount ?
                                    metas.head.items.filter(item => { if(filterKeys.includes(item.attrcode)) {item.visible = false} return true})
                                    :
                                    metas.head.items.filter(item => { if(filterKeys.includes(item.attrcode)) {item.visible = true} return true})
                metas.mtapp_detail.items =  isNoAmount ?
                                            metas.mtapp_detail.items.filter(item => { if(filterKeys.includes(item.attrcode)) {item.visible = false} return true})
                                            :
                                            metas.mtapp_detail.items.filter(item => { if(filterKeys.includes(item.attrcode)) {item.visible = true} return true})
                props.meta.setMeta(metas)

                if(data){
                    const setData = {
                        rows: data,
                        pageInfo
                    };
                    if (pageInfo.pageIndex == 0) {
                        setData.allpks = new Array(Number(pageInfo.total));
                    }
                    totalCount = data.length;
                    // this.props.transferTable.setTransferTableValue(presetVar.areaCode.billsArea, presetVar.areaCode.bodysArea, setData, 'pk_mtapp_bill', 'pk_mtapp_detail');
                    props.transferTable.setTransferTableValue(presetVar.areaCode.billsArea, presetVar.areaCode.bodysArea, setData, 'pk_mtapp_bill', 'pk_mtapp_detail');
                }else{
                    // this.props.transferTable.setTransferTableValue(presetVar.areaCode.billsArea, presetVar.areaCode.bodysArea, [], 'pk_mtapp_bill', 'pk_mtapp_detail');
                    props.transferTable.setTransferTableValue(presetVar.areaCode.billsArea, presetVar.areaCode.bodysArea, [], 'pk_mtapp_bill', 'pk_mtapp_detail');
                }

                isDefaultSearch != 'defaultSearch' && toast({
                    color: 'success',
                    title: multiLang && multiLang.get('201102BCLF-0055'),
                    content: (multiLang && multiLang.get('201102BCLF-0056') || '').replace('{count}', pageInfo && pageInfo.total || 0)
                })

            }
        })
    }
}

